<?php
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'check_user_login.php';
?>
<!DOCTYPE html>
<html>
    <head id="Head1">
        <title>
            Working Papers - Tesdo Admin
        </title>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
        ?>
        <script type="text/javascript">
            function call() {
                document.getElementById('hiddenMsg1').value = editor.getData();
                document.getElementById('hiddenMsg2').value = a_abstract.getData();
            }
        </script>
    </head>
    <body>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php';
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'ckeditor.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_pager.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_upload.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_data_input.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_journal.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_paper.php';
        ?>

        <script type="text/javascript">
            $(document).ready(function() {
                $('#a_dated_by_user').datepicker({dateFormat: "yy-mm-dd"});
            });
        </script>

        <?php
        //Script Process Page
        //Get objects
        $dao_paper = new dao_paper();
        $dao_journal = new dao_journal();
        $lib_pager = new lib_pager();
        $lib_upload = new lib_upload();
        $lib_data_input = new lib_data_input();

        //-------------Form_Load-------------

        $journals = $dao_journal->get_all();
//        var_dump($_POST);
        //CLEAR & CANCEL
        if (!empty($_POST['btnClear']) || !empty($_POST['btnCancel'])) {
            ?>
            <script type="text/javascript">
                window.location = <?php echo LINK_ROOT . DIR_ADMIN . 'journal_papers.php'; ?>;
            </script>
            <?php
        } else {

            //POST process
            if ($_SERVER['REQUEST_METHOD'] == "POST") {
                $action_type = $_POST['a_type'];
                $paper_id = empty($_POST['a_paper_id']) ? 0 : $_POST['a_paper_id'];

                if ($action_type == 'delete' && $paper_id != 0) {
                    if ($dao_paper->delete($paper_id) == 1) {
                        $success = 'Deleted paper with id = ' . $paper_id . ' successfully!';
                    } else {
                        $error = 'Delete paper with id = ' . $paper_id . ' failed!';
                    }
                } else if ($action_type == 'edit' && $paper_id != 0 && empty($_POST['btnSave'])) {
                    //Get Edit data when click on edit icon (not work with btnSave click)
                    $edit_item = $dao_paper->get_by_id($paper_id);
                } else if ((empty($_POST['a_dated_by_user']) ||
                        empty($_POST['a_title']) ||
                        empty($_POST['a_author']) ||
                        empty($_POST['a_keywords']) ||
                        empty($_POST['a_article_type']) ||
                        empty($_POST['a_publisher']) ||
                        empty($_POST['a_abstract']) ||
                        empty($_POST['a_ddl_journal'])) && !empty($_POST['btnSave'])) {
                    $error = "All the required field must not be empty!";
                } else {
                    $citation = "";
                    if (!empty($_POST['a_citation'])) {
                        $citation = $_POST['a_citation'];
                    }
                    $pages = "";
                    if (!empty($_POST['a_pages'])) {
                        $pages = $_POST['a_pages'];
                    }
                    $doi = "";
                    if (!empty($_POST['a_doi'])) {
                        $doi = $_POST['a_doi'];
                    }
                    $detail_article = "";
                    if (!empty($_POST['a_detail_article'])) {
                        $detail_article = $_POST['a_detail_article'];
                    }
                    
                    
                    //Get post array
                    $post_array = array(
                        'paper_id' => $paper_id,
                        'a_ddl_journal' => $_POST['a_ddl_journal'],
                        'a_title' => $_POST['a_title'],
                        'a_author' => $_POST['a_author'],
                        'a_citation' => $citation,
                        'a_keywords' => $_POST['a_keywords'],
                        'a_article_type' => $_POST['a_article_type'],
                        'a_pages' => $pages,
                        'a_doi' => $doi,
                        'a_publisher' => $_POST['a_publisher'],
                        'a_abstract' => $_POST['a_abstract'],
                        'a_detail_article' => $detail_article,
                        'a_acknown' => '',
                    );

                    //Get date
                    $post_array['a_dated_by_user'] = date('Y-m-d', strtotime($_POST['a_dated_by_user']));

                    if (!empty($_POST['a_pdf_file_stored'])) {
                        $post_array['a_pdf_file'] = $_POST['a_pdf_file_stored'];
                    } else if ($lib_upload->upload_file('a_pdf_file', DOCUMENT_ROOT . DIR_SHARED_UPLOAD_PDF_PAPERS)) {
                        $post_array['a_pdf_file'] = $_FILES['a_pdf_file']['name'];
                    }

                    //--------------
                    if ($action_type == 'insert' || $action_type == 'edit') {
                        if (!empty($post_array['a_pdf_file'])) {
                            if ($dao_paper->add(
                                            $post_array['paper_id'], $post_array['a_title'], $post_array['a_author'], $post_array['a_citation'], $post_array['a_keywords'], $post_array['a_article_type'], $post_array['a_pages'], $post_array['a_doi'], $post_array['a_publisher'], $post_array['a_acknown'], $post_array['a_pdf_file'], $post_array['a_abstract'], $post_array['a_detail_article'], $post_array['a_dated_by_user'], $post_array['a_ddl_journal'])) {
                                if ($action_type == 'insert') {
                                    $success = 'Inserted new paper category';
                                } else {
                                    $success = 'Updated information for paper category with id = ' . $paper_id;
                                }
                            } else {
                                $error = 'Failed to progess the request!';
                            }
                        } else {
                            $error = 'Failed, you must choose PDF File to upload!';
                        }
                    }
                }
            }
        }
        //Get list and divide page
        if (!empty($_GET['p'])) {
            $current_page = $_GET['p'];
        } else {
            $current_page = 1;
        }
        $list_total = $dao_paper->get_all();
        $page_size = 10;
        $total_page = $lib_pager->get_total_page($list_total, $page_size);
        $list_current = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);
        ?>

        <div>
            <div class="content-box-outer">
                <div class="content-box-right">
                </div>
                <div class="content-box-left">
                    <h1>Manage Papers</h1>
                    <div class="MessageArea">
                        <?php if (!empty($error)) { ?>
                            <span id="cphAdmin_lblMsg" style="color: red;">
                                <?php echo $error; ?>
                            </span>
                        <?php } ?>
                        <?php if (!empty($success)) { ?>
                            <span id="cphAdmin_lblMsg" style="color: green;">
                                <?php echo $success; ?>
                            </span>
                        <?php } ?>
                    </div>
                    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"  id="form_paper" onsubmit="call()" method="POST" enctype="multipart/form-data">
                        <div>
                            <input name="a_paper_id" value="<?php
                            if (empty($edit_item)) {
                                echo '0';
                            } else {
                                echo $edit_item['PaperID'];
                            }
                            ?>" type="hidden">

                            <input name="a_pdf_file_stored" value="<?php
                            if (empty($edit_item)) {
                                echo '';
                            } else {
                                echo $edit_item['PDFFiles'];
                            }
                            ?>" type="hidden">

                            <input name="a_type" value="<?php
                            if (empty($edit_item)) {
                                echo 'insert';
                            } else {
                                echo 'edit';
                            }
                            ?>" type="hidden">

                            <div class="TitleArea"><b>Select Journal:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <select name="a_ddl_journal" onchange="" id="ddl_paper_cat" style="width:300px;">
                                    <!--<option value="None" selected="selected">None</option>-->
                                    <?php if (empty($journals)) { ?>
                                        <script type="text/javascript">
                                            alert("Currently JOURNAL is empty, you must create JOURNAL before you can create PAPER!");
                                        </script>
                                        <?php
                                        lib_redirect::Redirect(DIR_ADMIN . 'thesis.php');
                                    }
                                    foreach ($journals as $item) {
                                        ?>
                                        <option value="<?php echo $item['JournalID'] ?>"><?php echo $item['Title'] ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                            <div class="TitleArea"><b>Paper Date:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input name="a_dated_by_user" value="<?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo date('Y-m-d', strtotime($edit_item['DateByUser']));
                                }
                                ?>" type="text" id="a_dated_by_user" style="width:200px;">
                            </div>
                            <div class="TitleArea"><b>Title:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input name="a_title" value="<?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['Title'];
                                }
                                ?>" type="text" id="a_title" style="width:200px;">
                            </div>
                            <div class="TitleArea"><b>Author:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input name="a_author" value="<?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['Author'];
                                }
                                ?>" type="text" id="a_author" style="width:200px;">
                            </div>
                            <div class="TitleArea"><b>Citation:</b></div>
                            <div class="textBoxArea">
                                <input name="a_citation" value="<?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['Citation'];
                                }
                                ?>" type="text" id="a_citation" style="width:400px;">
                            </div>
                            <div class="TitleArea"><b>Keywords:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input name="a_keywords" value="<?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['Keywords'];
                                }
                                ?>" type="text" id="a_keywords" style="width:400px;">
                            </div>
                            <div class="TitleArea"><b>Article Type:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input name="a_article_type" value="<?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['ArticleType'];
                                }
                                ?>" type="text" id="a_article_type" style="width:400px;">
                            </div>
                            <div class="TitleArea"><b>No of Pages:</b></div>
                            <div class="textBoxArea">
                                <input name="a_pages" value="<?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['Pages'];
                                }
                                ?>" type="text" id="a_pages" style="width:400px;">
                            </div>
                            <div class="TitleArea"><b>DOI:</b></div>
                            <div class="textBoxArea">
                                <input name="a_doi" value="<?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['DOI'];
                                }
                                ?>" type="text" id="a_doi" style="width:400px;">
                            </div>
                            <div class="TitleArea"><b>Publisher:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input name="a_publisher" value="<?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['Publisher'];
                                }
                                ?>" type="text" id="a_publisher" style="width:400px;">
                            </div>
                            <div class="TitleArea"><b>Abstract:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input type="hidden" name="hiddenMsg1" value="">
                                <textarea class="ckeditor" id="a_abstract" name="a_abstract">
                                <?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['Abstract'];
                                }
                                ?>
                                </textarea>
                                <script type="text/javascript">
                                   CKEDITOR.replace( 'a_abstract' );
                                   CKEDITOR.add            
                                </script>
                            </div>
                            <div class="TitleArea"><b>Detail Article:</b></div>
                            <div class="textBoxArea">
                                <input type="hidden" name="hiddenMsg2" value="">
                                <textarea class="ckeditor" id="a_detail_article" name="a_detail_article">
                                <?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['DetailArticle'];
                                }
                                ?>
                                </textarea>
                                <script type="text/javascript">
                                   CKEDITOR.replace( 'a_detail_article' );
                                   CKEDITOR.add            
                                </script>
                            </div>
                            <div class="TitleArea"><b>PDF File:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input type="file" name="a_pdf_file">
                                <?php if (!empty($edit_item)) { ?>
                                    <span>
                                        <a target="_blank" href="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_PDF_PAPERS . $edit_item['PDFFiles']; ?>"><?php echo $edit_item['PDFFiles']; ?></a>
                                    </span>
                                <?php }
                                ?>
                            </div>

                            <div class="TitleArea"><b>.</b></div>
                            <div class="textBoxArea">
                                <input type="submit" name="btnSave" value="Save" class="buttonDesign">
                                <input type="submit" name="btnClear" value="Clear" class="buttonDesign">
                                <input type="submit" name="btnCancel" value="Cancel" class="buttonDesign">
                            </div>
                        </div>

                        <br clear="all"><br>
                        <h1>Manage Paper</h1>
                        <div>
                            <div>
                                <table cellspacing="0" cellpadding="4" rules="cols" id="cphAdmin_gvJournal" style="color:Black;background-color:White;border-color:#DEDFDE;border-width:1px;border-style:None;width:988px;border-collapse:collapse;">
                                    <tbody><tr style="color:White;background-color:#6B696B;font-weight:bold;">
                                            <th scope="col">Paper Title</th>
                                            <th scope="col">Date</th>
                                            <th scope="col">Abstract</th>
                                            <th scope="col">PDF</th>
                                            <th scope="col">Actions</th>
                                        </tr>
                                        <?php
                                        $c = 0;
                                        foreach ($list_current as $i => $item) {
                                            if ($c % 2 == 0) {
                                                ?>
                                                <tr style="background-color:#F7F7DE;"> 
                                                <?php } else { ?>
                                                <tr style="background-color:#FFF;"> 
                                                    <?php
                                                }
                                                ?>
                                                <td>
                                                    <?php
                                                    if (!empty($item['Title'])) {
                                                        echo $item['Title'];
                                                    } else {
                                                        echo '';
                                                    }
                                                    ?>
                                                </td>
                                                <td>
                                                    <?php
                                                    if (!empty($item['DatedByUser'])) {
                                                        echo $item['DatedByUser'];
                                                    } else {
                                                        echo '';
                                                    }
                                                    ?>
                                                </td>
                                                <td>
                                                    <?php
                                                    if (!empty($item['Abstract'])) {
                                                        echo (strlen($item['Abstract']) > 100) ? substr($item['Abstract'], 0, 100) . '.....' : $item['Abstract'];
                                                    } else {
                                                        echo '';
                                                    }
                                                    ?>
                                                </td>
                                                <td>
                                                    <a target="_blank" href="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_PDF_PAPERS . $item['PDFFiles']; ?>">PDF</a>
                                                </td>
                                                <td align="center">
                                                    <input type="image" 
                                                           name="btnEdit"
                                                           title="Edit" 
                                                           onclick="set_value('a_type', 'edit');

                                                                       set_value('a_paper_id', <?php echo $item['PaperID']; ?>);
                                                                       submit_form('form_paper');" 
                                                           class="input-edit-image" 
                                                           src="<?php echo LINK_ROOT . DIR_ADMIN_LAYOUT_IMAGE . 'edit.png'; ?>" >
                                                    <input type="image" 
                                                           name="btnDelete" 
                                                           title="Delete" 
                                                           onclick="if (my_confirm()) {
                                                                           set_value('a_type', 'delete');
                                                                           set_value('a_paper_id', <?php echo $item['PaperID']; ?>);
                                                                           submit_form('form_paper');

                                                                       } else {
                                                                           return false;
                                                                       }"
                                                           class="input-delete-image" 
                                                           src="<?php echo LINK_ROOT . DIR_ADMIN_LAYOUT_IMAGE . 'delete.png'; ?>">
                                                </td>
                                            </tr>
                                            <?php
                                            $c++;
                                        }
                                        ?>
                                        <tr align="right" style="color:Black;background-color:#F7F7DE;">
                                            <td colspan="5">
                                                <table>
                                                    <tbody>
                                                        <tr>
                                                            <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                                                <td>
                                                                    <span>
                                                                        <a href="journal_papers.php?p=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                                    </span>
                                                                </td>
                                                            <?php } ?>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

        </div>

        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'footer.php';
        ?>
    </body>
</html>
